package com.ynu.springbootappdemo.service;

import cn.hutool.json.JSONUtil;
import com.ynu.springbootappdemo.dao.IGoodsDao;
import com.ynu.springbootappdemo.dao.ISkuItemDao;
import com.ynu.springbootappdemo.model.entity.Goods;
import com.ynu.springbootappdemo.model.entity.Properties;
import com.ynu.springbootappdemo.model.entity.SkuItem;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService {
    @Resource
    private  IGoodsDao goodsDao;

    @Resource
    private ISkuItemDao iSkuItemDao;

    public  List<Goods> getAllGoods(){
        return  goodsDao.findAll();
    }

    @Transactional
    public Goods getGoodsById(Long id){
        return goodsDao.getReferenceById(id);
    }

    public List<Properties> parserProp(String properties) {
        List<Properties> propertiesList = new ArrayList<>();

        //转为数组
        List<String> propList = JSONUtil.toList(properties, String.class);
        //遍历转型
        for (String prop : propList) {
            Properties properties1 = JSONUtil.toBean(prop, Properties.class);
            propertiesList.add(properties1);
        }
        return propertiesList;
    }

    public SkuItem getSkuById(Long id){
        return iSkuItemDao.getReferenceById(id);
    }

}
